// pages/personage/personage.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    wx_name: '',
    avatarUrl: defaultAvatarUrl,
    openid: '',
    phone: '', //手机号
    CUSTOMER_TEL: '', //手机号
    LEVEL_IMG: '', //会员图片
    INTEGRAL: '', //我的邮米
    Getphone: false,
    code: '',
    CUSTOMER_NAME: '',
    avatar: '',
    jindi_INTEGRAL: '', //进度条积分
    jindizongji: '', //总积分
    jifencha: '', //积分差
    CUSTOMER_ID: '',
    LEVELIMG: '',
    LEVEL_MATH: '',

    jiami: '',
    session_key: '',

    erweik: true,
  },

  zhuanjiejilu() {
    wx.navigateTo({
      url: '../../pages/zhuanjiematongji/zhuanjiematongji',
    })
  },


  zhuanjiema() {
    wx.navigateTo({
      url: '../../pages/zhuanjiemaerweiye/zhuanjiemaerweiye',
    })
  },




  xiugaige() {
    wx.navigateTo({
      url: '../../pages/newxiugaixingxi/newxiugaixingxi',
    })
  },


  erwima() {
    wx.navigateTo({
      url: '../../pages/newyonghuerweuma/newyonghuerweuma',
    })
  },


  more() {
    wx.navigateTo({
      url: '../../pages/more/more',
    })
  },


  shouhuodizhi() {

    wx.navigateTo({
      url: '../../pages/shouhuodizhi/shouhuodizhi',
    })
  },


  getSearchValue(e) {
    console.log(e.detail.value);
    wx.setStorageSync('wx_na', e.detail.value)
  },

  onChooseAvatar(e) {
    console.log(e.detail);
    const {
      avatarUrl
    } = e.detail
    this.setData({
      avatarUrl,
    })
    wx.uploadFile({
      filePath: avatarUrl,
      name: 'FIMG',
      url: 'https://jiagaojie.xajiarong.net/youchuhuiqin/Test/API_1169_uploadFileCOS', //服务器端接收图片的路径
      formData: {
        EMPLOYEE_ID: 1,
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      success: function (res) {
        console.log(JSON.parse(res.data).newFile); //发送成功回调
        const picture = JSON.parse(res.data)
        wx.setStorageSync('avatarUrl', picture.newFile)
        // this.setData({
        //   avatarUrl:picture.newFile,
        // })
      },
      fail: function (res) {
        console.log(res); //发送失败回调，可以在这里了解失败原因
      }
    })
  },





  //会员信息填写
  // huitiao() {
  //   wx.navigateTo({
  //     url: '../../pages/member/member',
  //   })

  // },



  huitiao() {
    wx.navigateTo({
      url: '../../pages/newquanyizhuce/newquanyizhuce',
    })

  },

  fankui() {


    wx.navigateTo({
      url: '../../pages/feedback/feedback',
    })

  },


  //跳转授权
  //  authorization() {
  //   var that = this
  //   wx.request({
  //     url: app.globalData.interface+'/newVersion/API_2007_getPhoneByOpenId',
  //     method: 'POST',
  //     //向后端发送的数据
  //     data: {
  //       OPEN_ID: this.data.openid  //将code发送到后台服务器。
  //     },
  //     header: {
  //       "Content-Type": "application/x-www-form-urlencoded"  //POST方式是这个
  //     },
  //     success: res => {
  //       console.log(res);
  //       console.log(res.data.data.CUSTOMER_TEL);
  //       if (res.data.result != 'success') {
  //         wx.showModal({
  //           title: '温馨提示',
  //           content: '请先绑定会员信息',
  //           success: function (res) {
  //             if (res.confirm) {//这里是点击了确定以后
  //               wx.navigateTo({
  //                 url: '../../pages/member/member',
  //               })
  //             } else {//这里是点击了取消以后
  //               console.log('用户点击取消')
  //             }
  //           }
  //         })
  //         that.setData({
  //           Getphone: false,
  //         })
  //       } else {
  //         var phone = res.data.data.CUSTOMER_TEL;
  //         app.globalData.phone = phone;
  //         that.setData({
  //           CUSTOMER_TEL: res.data.data.CUSTOMER_TEL,
  //           LEVEL_IMG: res.data.data.LEVEL_IMG,
  //           CUSTOMER_TEL: res.data.data.CUSTOMER_TEL,
  //           Getphone: true,
  //         })
  //       }
  //     }
  //   })

  // },




  orders() {
    wx.navigateTo({
      url: '../../pages/orders/orders?data=' + null,
    })
  },


  daifu() {
    wx.navigateTo({
      url: '../../pages/orders/orders?data=' + 0,
    })
  },



  jinxing() {
    wx.navigateTo({
      url: '../../pages/orders/orders?data=' + 1,
    })
  },



  yiwan() {
    wx.navigateTo({
      url: '../../pages/orders/orders?data=' + 2,
    })
  },



  //查看明细
  dingdan() {
    wx.navigateTo({
      url: '../../pages/wddingdan/wddingdan',
    })
  },


  search() {
    this.setData({
      CUSTOMER_ID: app.globalData.CUSTOMER_ID
    });

    console.log('用户ID', this.data.CUSTOMER_ID);

    wx.request({
      url: app.globalData.interface + '/newVersion/API_2010_selectUserSome',
      method: 'POST',
      data: {
        OPEN_ID: this.data.openid,
        CUSTOMER_ID: this.data.CUSTOMER_ID,
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data);
        console.log(res.data.data);
        if (res.data.data != null) {
          var CUSTOMER_TEL = res.data.data.CUSTOMER_TEL
          CUSTOMER_TEL = "" + CUSTOMER_TEL;
          var CUSTOMER_TEL = CUSTOMER_TEL.substr(0, 3) + "****" + CUSTOMER_TEL.substr(7)
          console.log(CUSTOMER_TEL)
          this.setData({
            myExchangeNUmber: res.data.data.myExchangeNUmber,
            CUSTOMER_NAME: res.data.data.CUSTOMER_NAME,
            avatar: res.data.data.CUSTOMER_IMG,
            CUSTOMER_TEL: CUSTOMER_TEL,
            LEVEL_IMG: res.data.data.LEVEL_IMG,
            LEVELIMG: res.data.data.LEVELIMG,
            INTEGRAL: res.data.data.INTEGRAL,
            LEVEL: res.data.data.LEVEL,
            jindi_INTEGRAL: res.data.data.INTEGRAL * 0.01,

            // jindizongji:res.data.data.INTEGRAL*10,
            jifencha: 1000 - res.data.data.INTEGRAL,
            LEVEL_MATH: res.data.data.LEVEL_MATH,

          })



        }

      }
    })
  },



  panduanyuan() {
    var that = this
    wx.request({
      url: app.globalData.interface + '/management/API3_1019_EmployeeLogo',
      method: 'POST',
      //向后端发送的数据
      data: {
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
      },
      success: res => {
        console.log(res);

        if (res.data.result != 'success') {


          console.log(22);
        } else {
          console.log(11);
          that.setData({
            erweik: false,
          })


        }
      }
    })

  },


  yuangong() {
    wx.navigateTo({
      url: '../../pages/newtotallogin/newtotallogin',
    })
  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {



    let value = wx.getStorageSync('avatarUrl')

    if (value) {
      this.setData({
        avatarUrl: value,

        CUSTOMER_ID: app.globalData.CUSTOMER_ID

      })
      console.log(this.data.CUSTOMER_ID);
    } else {
      this.setData({
        avatarUrl: defaultAvatarUrl,
        CUSTOMER_ID: app.globalData.CUSTOMER_ID
      })


    }




 






  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    this.setData({

      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,

    });








    console.log(this.data.jiami);

    var that = this

    if (that.data.jiami == '' || that.data.session_key == '') {
      console.log('1aaa');

      wx.login({
        success: function (res) {
          console.log(res);
          if (res.code) { //wx.login获取code。
            console.log(res.code);
            //发起网络请求
            wx.request({
              url: app.globalData.interface + '/post/API_1062_appGetOpenid',
              method: 'GET',
              //向后端发送的数据
              data: {
                code: res.code //将code发送到后台服务器。
              },
              header: {
                "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
              },
              success: res => {
                console.log(res);
                if (res.data.result != 'success') {
                  wx.showToast({
                    title: '请获取手机号',
                    duration: 1000
                  })
                } else {
                  console.log(res.data.openid);
                  that.setData({
                    openid: res.data.openid
                  })
                  // that.scanCod()
                  that.panduanyuan()
                  that.search()
                }
              }
            })
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });


    } else {
      console.log('成功');
      that.panduanyuan()
      that.search()
    }








    let wx_nm = wx.getStorageSync('wx_na')
    if (wx_nm) {
      this.setData({
        wx_name: wx_nm
      })
    } else {
      this.setData({
        wx_name: '微信用户',
      })
    }


    // setTimeout(() => {

    // }, 800)





  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})